package com.dormitory_springboot.controller;


import com.dormitory_springboot.entity.Building;
import com.dormitory_springboot.entity.Dormitory;
import com.dormitory_springboot.entity.Maintenance;
import com.dormitory_springboot.entity.Student;
import com.dormitory_springboot.form.SearchForm;
import com.dormitory_springboot.service.*;
import com.dormitory_springboot.util.ResultVOUtil;
import com.dormitory_springboot.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/maintenance")
public class MaintenanceController {

    @Autowired
    private BuildingService buildingService;
    @Autowired
    private DormitoryService dormitoryService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private MaintenanceService maintenanceService;

    @PostMapping("/save")
    public ResultVO save(@RequestBody Maintenance maintenance) {
        boolean save = this.maintenanceService.save(maintenance);
        if (!save) return ResultVOUtil.fail("保存失败");
        return ResultVOUtil.success(null);
    }

    @GetMapping("/list/{page}/{size}")
    public ResultVO list(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return ResultVOUtil.success(this.maintenanceService.list(page, size));
    }

    @GetMapping("/search")
    public ResultVO search(SearchForm searchForm) {
        return ResultVOUtil.success(this.maintenanceService.search(searchForm));
    }

    @GetMapping("/findById/{id}")
    public ResultVO findById(@PathVariable("id") Integer id) {
        return ResultVOUtil.success(this.maintenanceService.getById(id));
    }

    @GetMapping("/listByStudentId/{studentId}/{page}/{size}")
    public ResultVO listByStudentId(@PathVariable("studentId") Integer studentId,
                                    @PathVariable("page") Integer page,
                                    @PathVariable("size") Integer size) {
        return ResultVOUtil.success(this.maintenanceService.listByStudentId(studentId, page, size));
    }

//    @GetMapping("/findMaintenanceVOById/{id}")
//    public ResultVO findMaintenanceVOById(@PathVariable("id") Integer id) {
//        return ResultVOUtil.success(this.maintenanceService.findMaintenanceVOById(id));
//    }

    @PutMapping("/update")
    public ResultVO update(@RequestBody Maintenance maintenance) {
        boolean update = this.maintenanceService.updateById(maintenance);
        if (!update) return ResultVOUtil.fail("更新失败");
        return ResultVOUtil.success(null);
    }

    @DeleteMapping("/deleteById/{id}")
    public ResultVO deleteById(@PathVariable("id") Integer id) {
        Boolean delete = this.maintenanceService.removeById(id);
        if (!delete) return ResultVOUtil.fail("删除失败");
        return ResultVOUtil.success(null);
    }

    @GetMapping("/findDormitoryByStudentId/{id}")
    public ResultVO findDormitoryByStudentId(@PathVariable("id") Integer id) {
        Student student = this.studentService.getById(id);
        if (student == null) {
            return ResultVOUtil.fail("未找到对应的学生信息");
        }
        Integer dormitoryId = student.getDormitoryId();
        // 根据宿舍ID查询宿舍信息
        Dormitory dormitory = this.dormitoryService.getById(dormitoryId);
        if (dormitory == null) {
            return ResultVOUtil.fail("未找到对应的宿舍信息");
        }
        return ResultVOUtil.success(dormitory);
    }


    //需要返回的信息整合成一个包含学生、宿舍和宿舍楼id及对应名称的数据结构，然后返回该数据结构
    @GetMapping("/findBuildingByStudentId/{id}")
    public ResultVO findBuildingByStudentId(@PathVariable("id") Integer id) {
        Student student = this.studentService.getById(id);
        if (student == null) {
            return ResultVOUtil.fail("未找到对应的学生信息");
        }

        Integer dormitoryId = student.getDormitoryId();
        Dormitory dormitory = this.dormitoryService.getById(dormitoryId);
        if (dormitory == null) {
            return ResultVOUtil.fail("未找到对应的宿舍信息");
        }

        Integer buildingId = dormitory.getBuildingId();
        Building building = this.buildingService.getById(buildingId);
        if (building == null) {
            return ResultVOUtil.fail("未找到对应的宿舍楼信息");
        }

        // 返回包含学生id、姓名、宿舍id、宿舍名、宿舍楼id和宿舍楼名的Map
        Map<String, Object> data = new HashMap<>();
        data.put("studentId", student.getId());
        data.put("studentName", student.getName());
        data.put("dormitoryId", dormitory.getId());
        data.put("dormitoryName", dormitory.getName());
        data.put("buildingId", building.getId());
        data.put("buildingName", building.getName());

        return ResultVOUtil.success(data);
    }

}

